package com.xiaoy.javamaildemo.formwork.javamail;

import com.xiaoy.javamaildemo.JavaMailConf;
import com.xiaoy.javamaildemo.enums.FolderTypeEnum;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.event.MessageCountAdapter;
import javax.mail.internet.MimeMessage;
import javax.mail.search.SearchTerm;
import java.util.List;

/**
 * @ClassName AbstractMailContext
 * @Description TODO
 * @Author xiaoying
 * @Date 2022/6/28 14:42
 * @Version V1.0.0
 */
public abstract class AbstractMailContext implements IMail {

    protected Session session;

    protected Transport transport;

    protected JavaMailConf javaMailConf;

    protected Store imapStore;

    protected Store pop3Store;

    @Override
    public VevorMimeMessageBo getMessage(long messageNum) throws Exception {
        try {
           return getMessage(null,messageNum);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
        return null;
    }

    @Override
    public VevorMimeMessageBo getMessage(FolderTypeEnum folderTypeEnum, long messageNum) throws Exception {
        try {
            return doGetMessage(folderTypeEnum,messageNum);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
        return null;
    }

    @Override
    public List<VevorMimeMessageBo> pull() throws Exception {
        try {
            return pull(null,null);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
        return null;
    }

    @Override
    public List<VevorMimeMessageBo> pull(FolderTypeEnum folderTypeEnum, SearchTerm searchTerm) throws Exception {
        try {
            return doPull(folderTypeEnum,searchTerm);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
        return null;
    }

    @Override
    public void sendMail(MimeMessage mimeMessage) throws Exception {
        try {
            doSendMail(mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
    }

    @Override
    public Long createDrafts(MimeMessage mimeMessage) throws Exception{
        try {
           return doCreateDrafts(mimeMessage,null);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
        return null;
    }

    @Override
    public Long updateDrafts(MimeMessage mimeMessage, long draftId) throws Exception{
        try {
            return doUpdateDrafts(mimeMessage,draftId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
        return null;
    }

    @Override
    public void deleteDrafts(long draftId) throws Exception {
        try {
            doDeleteDrafts(draftId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
    }



    @Override
    public void sendDraft(long draftId) throws Exception{
        try {
            doSendDraft(draftId);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
    }

    @Override
    public void reply(long messageId, MimeMessage mimeMessage) throws Exception{
        try {
            doReply(messageId,mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
    }

    @Override
    public Long createDraftReply(long messageId,MimeMessage mimeMessage) throws Exception {
        try {
           return doCreateDraftReply(messageId,mimeMessage);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            close();
        }
        return null;
    }



    @Override
    public Session getSession() {
        return session;
    }

    /**
     * 按照文件夹和邮件ID获取邮件
     * @param folderTypeEnum
     * @param messageNum
     * @return
     * @throws Exception
     */
    protected abstract VevorMimeMessageBo doGetMessage(FolderTypeEnum folderTypeEnum,long messageNum) throws Exception;

    /**
     * 拉取指定邮箱，自定义搜索条件的邮件
     * @param folderTypeEnum
     * @param searchTerm
     * @return
     * @throws Exception
     */
    protected abstract List<VevorMimeMessageBo> doPull(FolderTypeEnum folderTypeEnum, SearchTerm searchTerm) throws Exception;

    /**
     * 发送邮件
     * @param mimeMessage
     * @throws Exception
     */
    protected abstract void doSendMail(MimeMessage mimeMessage) throws Exception;

    /**
     * 创建草稿
     * @param mimeMessage
     * @param messageCountAdapter
     * @throws Exception
     */
    protected abstract Long doCreateDrafts(MimeMessage mimeMessage, MessageCountAdapter messageCountAdapter) throws Exception;

    /**
     * 创建回复草稿
     * @param mimeMessage
     * @return
     * @throws Exception
     */
    protected abstract Long doCreateDraftReply(long messageId,MimeMessage mimeMessage) throws Exception;

    /**
     * 更新草稿
     * @param mimeMessage
     * @param draftId
     * @throws Exception
     */
    protected abstract Long doUpdateDrafts(MimeMessage mimeMessage,long draftId) throws Exception;

    /**
     * 发送草稿
     * @param draftId
     * @throws Exception
     */
    protected abstract void doSendDraft(long draftId) throws Exception;

    /**
     * 回复邮件
     * @param messageId
     * @param mimeMessage
     * @throws Exception
     */
    protected abstract void doReply(long messageId, MimeMessage mimeMessage) throws Exception;

    /**
     * 删除草稿
     * @param draftId
     */
    protected abstract void doDeleteDrafts(long draftId) throws MessagingException, Exception;
    private void close() throws Exception {
        if(transport.isConnected()){
            transport.close();
        }
        if(imapStore.isConnected()){
            imapStore.close();
        }
        if(pop3Store.isConnected()){
            pop3Store.close();
        }
    }

}
